package com.snail.billing.page;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.android.net.HttpResult;
import com.android.net.HttpSession;
import com.android.net.OnHttpResultListener;
import com.android.util.AlertUtil;
import com.android.util.ResUtil;
import com.snail.billing.BillingCallback;
import com.snail.billing.DataCache;
import com.snail.billing.Resource;
import com.snail.billing.data.Account;
import com.snail.billing.data.AccountManager;
import com.snail.billing.json.JsonBase;
import com.snail.billing.net.BillingHttpApp;
import com.snail.billing.os.DialogPage;
import com.snail.billing.session.BindMobileSession;
import com.snail.billing.session.GeneralLoginSession;
import com.snail.billing.util.BillingStringUtil;

public class BindMobilePage extends DialogPage implements View.OnClickListener, OnHttpResultListener {

	private TextView textTitle;
	private View buttonBack;

	private EditText inputMobile;
	private View buttonCaptcha;
	private EditText inputCaptcha;
	private View button;

	private Account account;
	private BindMobileSession bindMobileSession;
	private String mobile;
	private String captcha;

	private boolean isUsed;

	@Override
	public int createLayoutId() {
		return ResUtil.getLayoutId(Resource.layout.snailbilling_bind_mobile_activity);
	}

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		textTitle = (TextView) findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_text));
		textTitle.setText(ResUtil.getString(Resource.string.snailbilling_bind_title_mobile));
		buttonBack = findViewById(ResUtil.getViewId(Resource.id.snailbilling_title_button_back));
		buttonBack.setOnClickListener(this);
		//
		inputMobile = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_bind_mobile_input));
		buttonCaptcha = findViewById(ResUtil.getViewId(Resource.id.snailbilling_bind_mobile_button_captcha));
		buttonCaptcha.setOnClickListener(this);
		inputCaptcha = (EditText) findViewById(ResUtil.getViewId(Resource.id.snailbilling_bind_mobile_input_captcha));
		button = findViewById(ResUtil.getViewId(Resource.id.snailbilling_bind_mobile_button));
		button.setOnClickListener(this);
		//
		account = AccountManager.getCurrentAccount();
	}

	@Override
	public void onClick(View v) {
		if (v.equals(buttonBack)) {
			getPageManager().backward();
		} else if (v.equals(buttonCaptcha)) {
			isUsed = false;
			mobile = inputMobile.getText().toString();
			if (TextUtils.isEmpty((mobile))) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_bind_input_mobile));
				return;
			}
			if (!BillingStringUtil.isValidateMobile(mobile)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_bind_input_mobile2));
				return;
			}
			requestBindMobile(1);
		} else if (v.equals(button)) {
			if (isUsed) {
				inputMobile.setEnabled(true);
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_bind_validate_captcha));
				return;
			} else {
				isUsed = true;
			}
			captcha = inputCaptcha.getText().toString();
			if (TextUtils.isEmpty(captcha)) {
				AlertUtil.show(getContext(), ResUtil.getString(Resource.string.snailbilling_bind_input_captcha));
				return;
			}
			requestBindMobile(2);
		}
	}
	
	private void requestBindMobile(final int type) {
		BillingHttpApp billingHttpApp = new BillingHttpApp(getContext(), new BillingHttpApp.RequestHttpSesssionListener() {

			@Override
			public HttpSession getLoginHttpSession() {
				return new GeneralLoginSession(account.getAccount(), account.getPwd());
			}

			@Override
			public HttpSession getRequestHttpSesssion() {
				if (type == 1) {
					bindMobileSession = new BindMobileSession("1", mobile, null);
				} else if (type == 2) {
					bindMobileSession = new BindMobileSession("2", mobile, captcha);
				}
				return bindMobileSession;
			}
		});
		billingHttpApp.setOnHttpResultListener(this);
		billingHttpApp.request();
	}

	@Override
	public void onHttpResult(HttpResult httpResult) {
		if (httpResult.isSucceed()) {
			HttpSession httpSession = httpResult.getHttpSession();
			if (httpSession.equals(bindMobileSession)) {
				JsonBase jsonBase = new JsonBase((String) httpSession.getResponseData());
				Toast.makeText(getContext(), jsonBase.getMessage(), Toast.LENGTH_SHORT).show();
				if (jsonBase.getCode() == 1) {
					if (bindMobileSession.getType().equals("1")) {
						inputMobile.setEnabled(false);
					} else if (bindMobileSession.getType().equals("2")) {
						getPageManager().backward();
						String action = BillingCallback.ACTION_BIND_MOBILE;
						DataCache.getInstance().importParams.billingCallback.onCallback(1, action, new String[] { mobile });
					}
				}
			}
		}
	}

}
